package com.jt.aop;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.vo.SysResult;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice       //作用:标识我是一个通知的方法,并且只拦截controller层的异常,并且返回JSON.
public class SysResultException {
    //定义一个全局方法,
    //JSONP的异常处理应该是callback({status:201,msg:"",data:""})
    //
    @ExceptionHandler(RuntimeException.class)  //配置异常的类型,当遇到了某种异常时,才执行该方法
    public Object exception(Exception e, HttpServletRequest request) {
String callback = request.getParameter("callback");
if(!StringUtils.isEmpty(callback)){
    return new JSONPObject(callback,SysResult.fail());
}
        e.printStackTrace();//日志记录/控制台输出,方便知道哪里报错
        return SysResult.fail();
    }
}
